<?
  class Phpbb
  {
      function lastPosts($posts_count = 10, $forums = array())
      {
      	global $db, $auth, $user; //, $template;
      	global $phpbb_root_path, $phpEx, $config;
        
        $user->setup('viewforum');
        
      	$forum_rows = $subforums = $forum_ids = $forum_ids_moderator = $forum_moderators = $active_forum_ary = array();
      	$parent_id = $visible_forums = 0;
      	$sql_from = '';
      
      	// Display list of active topics for this category?
      	$show_active = (isset($root_data['forum_flags']) && ($root_data['forum_flags'] & FORUM_FLAG_ACTIVE_TOPICS)) ? true : false;
      
      	$sql_array = array(
      		'SELECT'	=> 'f.*',
      		'FROM'		=> array(
      			FORUMS_TABLE		=> 'f'
      		),
      		'LEFT_JOIN'	=> array(),
      	);
      
      	if ($config['load_db_lastread'] && $user->data['is_registered'])
      	{
      		$sql_array['LEFT_JOIN'][] = array('FROM' => array(FORUMS_TRACK_TABLE => 'ft'), 'ON' => 'ft.user_id = ' . $user->data['user_id'] . ' AND ft.forum_id = f.forum_id');
      		$sql_array['SELECT'] .= ', ft.mark_time';
      	}
      	else if ($config['load_anon_lastread'] || $user->data['is_registered'])
      	{
      		$tracking_topics = (isset($_COOKIE[$config['cookie_name'] . '_track'])) ? ((STRIP) ? stripslashes($_COOKIE[$config['cookie_name'] . '_track']) : $_COOKIE[$config['cookie_name'] . '_track']) : '';
      		$tracking_topics = ($tracking_topics) ? tracking_unserialize($tracking_topics) : array();
      
      		if (!$user->data['is_registered'])
      		{
      			$user->data['user_lastmark'] = (isset($tracking_topics['l'])) ? (int) (base_convert($tracking_topics['l'], 36, 10) + $config['board_startdate']) : 0;
      		}
      	}
      
      	if ($show_active)
      	{
      		$sql_array['LEFT_JOIN'][] = array(
      			'FROM'	=> array(FORUMS_ACCESS_TABLE => 'fa'),
      			'ON'	=> "fa.forum_id = f.forum_id AND fa.session_id = '" . $db->sql_escape($user->session_id) . "'"
      		);
      
      		$sql_array['SELECT'] .= ', fa.user_id';
      	}
      
      	$sql = $db->sql_build_query('SELECT', array(
      		'SELECT'	=> $sql_array['SELECT'],
      		'FROM'		=> $sql_array['FROM'],
      		'LEFT_JOIN'	=> $sql_array['LEFT_JOIN'],
      
      		'WHERE'		=> $sql_where,
      
      		'ORDER_BY'	=> 'f.left_id',
      	));
      
      	$result = $db->sql_query($sql);
      
      	$forum_tracking_info = array();
      	$branch_root_id = $root_data['forum_id'];
      
      	// Check for unread global announcements (index page only)
      	$ga_unread = false;
      	if ($root_data['forum_id'] == 0)
      	{
      		$unread_ga_list = get_unread_topics($user->data['user_id'], 'AND t.forum_id = 0', '', 1);
      
      		if (!empty($unread_ga_list))
      		{
      			$ga_unread = true;
      		}
      	}
      
        // CUSTOM: id of ignored forums
        $ignore_forums = array(33);
      
        $post_num = 0;
        $forum_ids = array();
      	while ($row = $db->sql_fetchrow($result))
      	{
      		$forum_id = $row['forum_id'];
        	// skip ignored forums
      	  if (in_array($forum_id, $ignore_forums))
      	  {
            continue;
          }
      
      		// Category with no members
      		if ($row['forum_type'] == FORUM_CAT && ($row['left_id'] + 1 == $row['right_id']))
      		{
      			continue;
      		}
      
          // TODO: ???
      		// Skip branch
      		if (isset($right_id))
      		{
      			if ($row['left_id'] < $right_id)
      			{
      				continue;
      			}
      			unset($right_id);
      		}
      
      		if (!$auth->acl_get('f_list', $forum_id))
      		{
      			// if the user does not have permissions to list this forum, skip everything until next branch
      			$right_id = $row['right_id'];
      			continue;
      		}
      		
      		// for special forums
      		if (!empty($forums))
      		{
            if (!in_array($forum_id, $forums))
            {
              continue;
            }
          }
      
          // remember forum id
          $forum_ids[] = $forum_id; 
      	}
      	$db->sql_freeresult($result);
      
      	$sql_array = array(
      		'SELECT'	=> 'f.*',
      		'FROM'		=> array(
      			TOPICS_TABLE		=> 'f'
      		),
      		'WHERE'	=> "forum_id in(".join(', ', $forum_ids).")",
      	);
      
        $sql = $db->sql_build_query('SELECT', array(
      		'SELECT'	=> $sql_array['SELECT'],
      		'FROM'		=> $sql_array['FROM'],
      		'WHERE'		=> $sql_array['WHERE'],
      		'ORDER_BY'	=> 'topic_last_post_time DESC LIMIT '.$posts_count
      	));
      	$topics = $db->sql_query($sql);
      	
      	$topics_data = array();
      	
      	while($topic = $db->sql_fetchrow($topics))
      	{
      		$last_topic_title_full = htmlspecialchars_decode(censor_text($topic['topic_title']));
      		$last_topic_title = (utf8_strlen($last_topic_title_full) > 40) ? trim(utf8_substr($last_topic_title_full, 0, 40)) . '…' : $last_topic_title_full;
      		$last_topic_url = append_sid("{$phpbb_root_path}viewtopic.$phpEx", 'p=' . $topic['topic_last_post_id'] . '#p' . $topic['topic_last_post_id']);
      
      		$last_post_time = $user->format_date($topic['topic_last_post_time']);  
          
          $topics_data[] = array(
      				'TOPIC_TITLE'		=> htmlspecialchars($last_topic_title),
      				'TOPIC_TITLE_FULL'	=> htmlspecialchars($last_topic_title_full),
      				'U_TOPIC_URL'		=> $last_topic_url,
      				'POSTER_NAME' => $topic['topic_last_poster_name'],
      				'U_POSTER'		=> get_username_string('profile', $topic['topic_last_poster_id'], $topic['topic_last_poster_name'], $topic['topic_last_poster_colour']),
      				'POST_TIME' => $last_post_time
      		);
        }
        $db->sql_freeresult($topics);
        return $topics_data;
      }                             
  }
?>